from rest_framework import serializers
from apps.answer.models import Label, Question, Reply
from apps.head.models import Article
from apps.recruit.models import Enterprise
from apps.users.models import User


# 标签简单数据序列化器
class LabelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ('id', 'label_name')


# 用户简单数据序列化器
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')


# 文章简单数据
class ArticlesSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'title')


# 用户简单数据序列化器
class UserSimpleSerializer(serializers.ModelSerializer):
    articles = ArticlesSimpleSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles', 'fans')


# 关于问题序列化器
class QuestionsListSerializer(serializers.ModelSerializer):
    labels = LabelSerializer(read_only=True, many=True)
    user = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Question
        fields = '__all__'


# 父评论
class ReplyParentSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)

    class Meta:
        model = Reply
        fields = ('id', 'content', 'createtime', 'useful_count', 'unuseful_count', 'user')


# 关于回复序列化器
class AnswerQuestionListSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    parent = ReplyParentSerializer(read_only=True, many=True)
    subs = ReplyParentSerializer(read_only=True, many=True)

    class Meta:
        model = Reply
        fields = ('id', 'content', 'createtime', 'userful_count', 'unuserful_count', 'parent', 'problem', 'user', 'subs')


# 关于文章头条序列化器
class ArticlesSerializer(serializers.ModelSerializer):
    user = UserSimpleSerializer(read_only=True)
    collected = serializers.BooleanField(default=False)
    image = serializers.CharField(read_only=True)

    class Meta:
        model = Article
        fields = ('id', 'title', 'content', 'createtime', 'user', 'collected_users', 'collected', 'image')


# 关于企业招聘序列化器
class EnterpisesSerializer(serializers.ModelSerializer):
    recruits = serializers.IntegerField(read_only=True)

    class Meta:
        model = Enterprise
        fields = ('id', 'name', 'labels', 'logo', 'recruits', 'summary')


# 用户中心序列化器
class UserCenterSerializer(serializers.ModelSerializer):

    username = serializers.CharField(read_only=True)
    labels = LabelSerializer(read_only=True, many=True)
    questions = QuestionsListSerializer(read_only=True, many=True)
    answer_question = AnswerQuestionListSerializer(read_only=True, many=True)
    collected_articles = ArticlesSerializer(read_only=True, many=True)
    enterpises = EnterpisesSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'mobile', 'realname', 'birthday', 'sex', 'avatar', 'website', 'email', 'city',
                  'address', 'labels', 'questions', 'answer_question', 'collected_articles', 'enterpises')


# 修改密码序列化器
class UpdatePasswordSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('password',)
        extra_kwargs = {
            'password': {'write_only': True}
        }

    def update(self, instance, validated_data):

        user = super().update(instance, validated_data)
        password = validated_data.get('password')
        user.set_password(password)
        user.save()

        return user


# 修改擅长标签
class UpdateLabelSerializer(serializers.ModelSerializer):
    labels = serializers.PrimaryKeyRelatedField(required=True, many=True, queryset=Label.objects.all())

    class Meta:
        model = User
        fields = ('id', 'labels')
        


